package com.nipun.graph.objects;

import java.util.Collection;
import java.util.HashMap;

public class GraphImpl implements Graph {
	
	private HashMap graph;
	
	private HashMap nodes;
	
	private HashMap edges;
	
	private int edgeNum;
	
	public GraphImpl(){
		graph = new HashMap();
		nodes = new HashMap();
		edges = new HashMap();
		edgeNum = 0;
	}
	
	/* (non-Javadoc)
	 * @see com.nipun.graph.objects.Graph#addNode(com.nipun.graph.objects.Node)
	 */
	public void addNode(Vertex vertex){
		if(vertex.getLabel()==null || "".equals(vertex.getLabel())){
			System.out.println("node had no label...did not add to graph");
			return;
		}
		nodes.put(vertex.getLabel(), vertex);
	}
	
	/* (non-Javadoc)
	 * @see com.nipun.graph.objects.Graph#addEdge(com.nipun.graph.objects.Edge)
	 */
	public int addEdge(Edge e){
		edges.put(new Integer(edgeNum), e);
		edgeNum++;
		return edgeNum;
	}
	
	/* (non-Javadoc)
	 * @see com.nipun.graph.objects.Graph#deleteNode(java.lang.String)
	 */
	public void deleteNode(String name){
		if(nodes.get(name) == null){
			return;
		}else{
			nodes.remove(name);
		}
	}
	
	/* (non-Javadoc)
	 * @see com.nipun.graph.objects.Graph#deleteEdge(int)
	 */
	public void deleteEdge(int e){
		if(edges.get(new Integer(e)) == null){
			return;
		}else{
			edges.remove(new Integer(e));
		}
	}
	
	public Vertex lookUpNode(String label){
		Vertex result = null;
		result = (Vertex)nodes.get(label);
		return result;
	}
	
	public String toString(){
		String result = "";
		Collection nodesCol = nodes.values();
		Object[] nodesArr = nodesCol.toArray();
		for(int i=0; i<nodesArr.length;i++){
			result=result+nodesArr[i].toString()+"\n";
		}
		Collection edgeCol = edges.values();
		Object[] edgeArr = edgeCol.toArray();
		for(int i=0;i<edgeArr.length;i++){
			result=result+edgeArr[i].toString()+"\n";
		}
		
		return result;
	}

	public Object[] getEdges() {
		Object[] edgeArr = null;
		Collection col = edges.values();
		edgeArr = col.toArray();
		return edgeArr;
	}

	public Object[] getVertices() {
		Object[] vArr = null;
		Collection col = nodes.values();
		vArr = col.toArray();
		return vArr;
	}

}
